package com.imay.live.manager;

import com.imay.live.helper.EventHelper;
import com.imay.tbas_api.IMYTBASObject;

/**
 * Created by Administrator on 2017/8/24 0024.
 */

public class DataSetManager {

    public static final String ACTION_LOAD_DATA = "action_load_data";
    public static final String ACTION_CLEAR_DATA = "action_clear_data";

    private IMYTBASObject mDataNotify;

    public DataSetManager() {

    }

    public static class Builder {

        private final DataSetListener listener;

        public static DataSetManager.Builder create(DataSetListener listener) {
            if (listener == null) {
                throw new NullPointerException("listener == null");
            }
            return new DataSetManager.Builder(listener);
        }

        protected Builder(DataSetListener listener) {
            this.listener = listener;
        }

        public DataSetManager build() {
            DataSetManager dataSetManager = new DataSetManager();
            dataSetManager.registerDataNotify(listener);
            return dataSetManager;
        }
    }

    /**
     * 加载数据
     */
    public static void loadData() {
        EventHelper.postDataSet(ACTION_LOAD_DATA);
    }

    /**
     * 清空数据
     */
    public static void clearData() {
        EventHelper.postDataSet(ACTION_CLEAR_DATA);
    }


    /**
     * 用户失效的通知
     */
    public void registerDataNotify(final DataSetListener listener) {
        mDataNotify = new IMYTBASObject(false);
        mDataNotify.stringCommand = EventHelper.EVENT_DATASET;
        mDataNotify.setDataResultListener(new IMYTBASObject.OnDataResultListener() {
            @Override
            public void onCommandTaskSuccess(IMYTBASObject object) {
                if (ACTION_LOAD_DATA.equals(object.stringData)) {
                    listener.loadData();
                } else if (ACTION_CLEAR_DATA.equals(object.stringData)) {
                    listener.clearData();
                }
            }
        });
        mDataNotify.addEventListenWithTBASObject();
    }


    public void unregisterDataNotify() {
        if (mDataNotify != null) {
            mDataNotify.destroy();
        }
    }
}
